package org.example.service;

import org.example.ioc.annotation.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service("timeDisplayService")
public class TimeDisplayService {

    private ExecutorService executorService;

    public TimeDisplayService() {
        // 创建固定大小的线程池
        this.executorService = Executors.newFixedThreadPool(3);
    }

    public void displayCurrentTimeAsync() {
        CompletableFuture.runAsync(() -> {
            String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            System.out.println("异步获取当前时间: " + currentTime);
        }, executorService);
    }

    public CompletableFuture<String> getCurrentTimeAsync() {
        return CompletableFuture.supplyAsync(() -> {
            try {
                // 模拟耗时操作
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }, executorService);
    }

    public void shutdown() {
        if (executorService != null && !executorService.isShutdown()) {
            executorService.shutdown();
        }
    }
}
